﻿var rec = 0;
$(document).ready(function () {

    $("#add-link").on("click", function () {
        $('#dialog').load('../form/sysUserForm.aspx').dialog({ autoOpen: false, modal: true, width: 600, title: 'Add User' });
        $('#dialog').dialog('open');

    });

    var datasource = new kendo['data'].DataSource({
        type: 'json',
        serverPaging: true,
        serverSorting: true,
        pageSize: 10,
        transport: {
            read: {
                url: '../Handler/sysUserHandler.ashx?show=read',
                dataType: 'json'
            }
        },
        schema: {
            data: 'data',
            total: 'pageSize'
        }
    });

    $('.editGrid')['live']('click', function () {
        var Id = $(this)['attr']('rel');
        $('#dialog').load('../form/sysUserForm.aspx?Id=' + Id).dialog({ autoOpen: false, modal: true, width: 600, title: 'Edit User' });
        $('#dialog').dialog('open');
        return false;
    });

    $('.deleteGrid')['live']('click', function () {
        var Id = $(this)['attr']('rel');
        if (confirm('Are you sure?')) {
            UIBlock_nonTimeout('Deleting..');
            $['ajax']({
                url: '../Handler/sysUserHandler.ashx?show=delete&Id=' + Id,
                type: 'get',
                dataType: 'json',
                success: function () {
                    $('#user-grid')['data']('kendoGrid')['dataSource']['read']();
                    $['unblockUI']();
                },
                error: function () {
                    UIBlock('Error while deleting data!');
                }
            });

        }


        return false;
    });

    $('#user-grid').kendoGrid({
        dataSource: datasource,
        columns: [
        //{
        //            title: '&nbsp;',
        //            template: '#= ++rec #',
        //            width: 30,
        //            attributes: {
        //                style: 'text-align: center'
        //            }
        //        },

         {
         title: '&nbsp;',
         field: 'AppID',
         width: 20,
         template: '<a href=\'\' class=\'editGrid\' rel=\'${UserID}\' title=\'Edit\'></a>',
         width: 20,
         attributes: {
             style: 'text-align: center'
         }
     }, {
         title: '&nbsp;',
         field: 'AppID',
         width: 20,
         template: '<a href=\'\' class=\'deleteGrid\' rel=\'${UserID}\' title=\'Delete\'></a>',
         width: 20,
         attributes: {
             style: 'text-align: center'
         }
     }, {
         title: 'Name',
         field: 'Name',
         width: 100
     }, {
         title: 'Email',
         field: 'UserCode',
         width: 100,
         attributes: {
             style: 'text-align: left'
         }
     }, {
         title: 'Group',
         field: 'Groups.Value.GroupName',
         width: 100,
         attributes: {
             style: 'text-align: left'
         }
     }
     
//     , {
//         title: 'Group App',
//         field: 'GroupsApplication.Value.AppGroupName',
//         width: 100,
//         attributes: {
//             style: 'text-align: left'
//         }
//     }
     
     ],
        groupable: true,
        sortable: true,
        resizable: true,
        pageable: {
            refresh: true,
            pageSizes: true
        },
        selectable: 'row'
        //        ,
        //        dataBound: function () {
        //            $['ajax']({
        //                url: '../Handler/AuthorizationHandler.ashx',
        //                type: 'POST',
        //                async: false,
        //                dataType: 'json',
        //                data: 'CurrentURL=' + document['location'],
        //                success: function (_0x6b0bx8) {
        //                    if (!_0x6b0bx8['AllowCreate']) {
        //                        $('.create-button')['hide']();
        //                    };
        //                    if (!_0x6b0bx8['AllowUpdate']) {
        //                        $('.editGrid')['hide']();
        //                    };
        //                    if (!_0x6b0bx8['AllowDelete']) {
        //                        $('.deleteGrid')['hide']();
        //                    };
        //                },
        //                error: function () {
        //                    UIBlock('Error while checking authorization user!');
        //                }
        //            });
        //        }
    });

});